import os

from ocr.basic_model_module.images_classifier import DocumentImagesClassifier
from ocr.main_stream_controller import MainStreamController
from ocr.utils.tools import parse_yaml


class ModelInit(object):
    def __init__(self, config_file):
        self.config = parse_yaml(config_file)
        self.document_images_classifier = DocumentImagesClassifier(self.config["document_images_classifier_model_path"])


class DocumentImagesClassification(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = object.__new__(cls)
        return cls.__instance

    def __init__(self, base_path):
        ocr_path = os.path.join(base_path, "ocr")
        config_path = os.path.join(ocr_path, "config.yaml")

        self.model_manager = ModelInit(config_path)

        self.main_stream_controller = MainStreamController(self.model_manager)

    def process(self, img_data):
        results = self.main_stream_controller.process(img_data)

        return results
